package com.rabbitmq.example.config;

import com.rabbitmq.example.dao.MessageMapper;
import com.rabbitmq.example.idempotent.IdempotentConsumerAspect;
import com.rabbitmq.example.retry.RetryMessageAspect;
import com.rabbitmq.example.service.RetryMessageRabbitTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.redis.core.StringRedisTemplate;

@ConditionalOnProperty(prefix = "rabbitmq.retrysend", value = "enabled", havingValue = "true",
        matchIfMissing = false)
@AutoConfigureAfter({RabbitAutoConfiguration.class, MybatisAutoConfiguration.class})
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@MapperScan("com.rabbitmq.example.dao")
public class RetrySendMessageAutoConfig {

    @Bean
    public com.rabbitmq.example.service.RetryMessageRabbitTemplate retryMessageService(RabbitTemplate rabbitTemplate, MessageMapper messageMappper) {
       return new RetryMessageRabbitTemplate(rabbitTemplate,messageMappper);
    }

    @Bean
    public RetryMessageAspect retryMessageAspect(RetryMessageRabbitTemplate retryMessageService) {
        return new RetryMessageAspect(retryMessageService);
    }

    @Bean
    public IdempotentConsumerAspect idempotentConsumerAspect(StringRedisTemplate stringRedisTemplate) {
        return new IdempotentConsumerAspect(stringRedisTemplate);
    }
}
